﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

namespace TechControl.BD
{
    class MarcaDB: BaseDB
    {
        private static MarcaDB instance;

        private MarcaDB() { }

        public static MarcaDB getInstance()
        {
            if (instance == null)
            {
                instance = new MarcaDB();
            }
            return instance;
        }

        //retorna todas as marcas na base.
        public ArrayList selectAllMarcas()
        {
            ArrayList result = new ArrayList();
            try
            {
                this.open();
                SqlCommand command = new SqlCommand("select * from tb_marca;", this.conn);
                SqlDataReader rdr = command.ExecuteReader();

                while (rdr.Read())
                {
                    result.Add(rdr.GetInt64(0).ToString() +"= "+rdr.GetString(1));
                }
            }
            finally
            {
                this.close();
            }
            return result;
        }

        //inseri um novo registro.
        public bool insertNewMarca(Int64 idMarca, string marca)
        {
            bool result = true;

            try
            {
                this.open();
                SqlCommand command = new SqlCommand("insert into tb_marca values('" + idMarca + "', '" + marca + "') ", this.conn);
                command.ExecuteNonQuery();
            }
            catch (Exception) { result = false; }
            finally { this.close(); }
            return result;
        }

        //retorna ultimo registro inserido na tabela.
        public Int64 getLastCodMarca()
        {
            Int64 result = 0;
            try
            {
                this.open();
                SqlCommand command = new SqlCommand("select MAX(id_marca) from tb_marca;", this.conn);
                SqlDataReader rdr = command.ExecuteReader();
                while (rdr.Read())
                {
                    result = rdr.GetInt64(0);
                }
            }
            catch (Exception)
            { result = 0; }
            finally { this.close(); }
            return result;
        }
    }
}
